﻿//**************************************************************************//
// Copyright (C) Abdul Rasheed. All rights Reserved.                        //
// rasheedat.blogspot.com                                                   //
//**************************************************************************//
using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PPHR.Common;
using PPHR.DataLogic;
using System.IO;
using System.Reflection;

namespace PPHR
{
    public partial class VisitReports : Form
    {
        #region Constructor
        public VisitReports()
        {
            InitializeComponent();
        }
        #endregion Constructor

        #region Events
        private void VisitReports_Load(object sender, EventArgs e)
        {
            try
            {
                CommonDataLogic commonLogic = new CommonDataLogic();
                chkFromDate.Checked = true;
                chkToDate.Checked = true;
                //Load Combo Box
                DataTable dtVisitType = commonLogic.GetMasterTable("VISITTYP");
                cboVisitType.DataSource = dtVisitType;
                cboVisitType.ValueMember = dtVisitType.Columns[0].ColumnName;
                cboVisitType.DisplayMember = dtVisitType.Columns[1].ColumnName;
                cboVisitType.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Constants.MessageBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
            }
        }

        private void menuItemHome_Click(object sender, EventArgs e)
        {
            try
            {
                this.Hide();
                PPHR phr = new PPHR();
                phr.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Constants.MessageBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
            }
        }

        private void menuItemClose_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Constants.MessageBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
            }
        }

        private void menuItemShow_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateForm())
                {
                    VisitReportData PVReport = new VisitReportData();
                    ReportsDataLogic report = new ReportsDataLogic();
                    if (chkFromDate.Checked)
                    {
                        PVReport.StartDate = dtpFromDate.Value;
                    }
                    if (chkToDate.Checked)
                    {
                        PVReport.EndDate = dtpToDate.Value;
                    }
                    if (!txtClinicName.Text.Trim().Equals(string.Empty))
                    {
                        PVReport.FacilityName = txtClinicName.Text.Trim();
                    }
                    if (!txtDrName.Text.Trim().Equals(string.Empty))
                    {
                        PVReport.DrName = txtDrName.Text.Trim();
                    }
                    if (!cboVisitType.SelectedIndex.Equals(0))
                    {
                        PVReport.VisitType = ((System.Data.DataRowView)(cboVisitType.SelectedItem)).Row.ItemArray[0].ToString();
                    }
                    DataTable dtVisits = report.VisitReportHeader(PVReport);
                    if (dtVisits != null)
                    {
                        CreateVisitReport(dtVisits);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Constants.MessageBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
            }
        }

        private void chkFromDate_CheckStateChanged(object sender, EventArgs e)
        {
            try
            {
                if (chkFromDate.Checked)
                {
                    dtpFromDate.Enabled = true;
                }
                else
                {
                    dtpFromDate.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Constants.MessageBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
            }
        }

        private void chkToDate_CheckStateChanged(object sender, EventArgs e)
        {
            try
            {
                if (chkToDate.Checked)
                {
                    dtpToDate.Enabled = true;
                }
                else
                {
                    dtpToDate.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Constants.MessageBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
            }
        }
        #endregion Events

        #region General Methods
        private bool ValidateForm()
        {
            if (chkFromDate.Checked && chkToDate.Checked)
            {
                if (dtpFromDate.Value > dtpToDate.Value)
                {
                    MessageBox.Show("Visit from date can't be greater than Visit to date.", Constants.MessageBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
            }
            return true;
        }
        
        /// <summary>
        /// Create visit report
        /// </summary>
        /// <param name="dtVisits"></param>
        private void CreateVisitReport(DataTable dtVisits)
        {
            StringBuilder sb = new StringBuilder();
            VisitDataLogic PVLogic = new VisitDataLogic();
            sb.Append("<html>");
            sb.Append("<head>");
            sb.Append("<title></title>");
            sb.Append("</head>");
            sb.Append("<body>");
            if (dtVisits.Rows.Count == 0)
            {
                sb.Append("No Record(s) found");
            }
            else
            {
                for (int i = 0; i < dtVisits.Rows.Count; i++)
                {
                    sb.Append("    <div id=\"Visit" + i.ToString() + "\">");
                    sb.Append("        <table border='1px' style=\"font-size: 9pt; color: #000000; background-color: #CDFEE7;");
                    sb.Append("            border: 1px solid #006600; font-family: Arial; width: 625px;\">");
                    sb.Append("            <tr>");
                    sb.Append("                <td style=\"width: 75px;\">");
                    sb.Append("                    Visit ID");
                    sb.Append("                </td>");
                    sb.Append("                <td style=\"width: 220px;\">");
                    sb.Append(dtVisits.Rows[i]["VisitNumber"].ToString());
                    sb.Append("                </td>");
                    sb.Append("                <td style=\"width: 75px;\">");
                    sb.Append("                    Date");
                    sb.Append("                </td>");
                    sb.Append("                <td>");
                    sb.Append(dtVisits.Rows[i]["VisitDate"].ToString());
                    sb.Append("                </td>");
                    sb.Append("            </tr>");
                    sb.Append("            <tr>");
                    sb.Append("                <td style=\"width: 75px;\">");
                    sb.Append("                    Dr. Name");
                    sb.Append("                </td>");
                    sb.Append("                <td style=\"width: 220px;\">");
                    sb.Append(dtVisits.Rows[i]["ConsultantName"].ToString());
                    sb.Append("                </td>");
                    sb.Append("                <td style=\"width: 75px;\">");
                    sb.Append("                    Clinic/Hospital");
                    sb.Append("                </td>");
                    sb.Append("                <td>");
                    sb.Append(dtVisits.Rows[i]["FacilityName"].ToString());
                    sb.Append("                </td>");
                    sb.Append("            </tr>");
                    sb.Append("        </table>");
                    sb.Append("        <br />");
                    sb.Append("        <div id=\"VisitDetail" + i.ToString() + "\">");
                    sb.Append("            <table style=\"font-size: 9pt; color: #000000; background-color: #CAE4FF; border: 1px dotted #006600;");
                    sb.Append("                font-family: Arial; width: 625px;\">");
                    sb.Append("                <tr>");
                    sb.Append("                    <td style=\"width: 145px;\" valign=\"top\">");
                    sb.Append("                        Visit Detail:");
                    sb.Append("                    </td>");
                    sb.Append("                    <td>");
                    sb.Append(dtVisits.Rows[i]["VisitDetail"].ToString());
                    sb.Append("                    </td>");
                    sb.Append("                </tr>");
                    sb.Append("            </table>");
                    sb.Append("            <br />");
                    sb.Append("            <table style=\"font-size: 9pt; color: #000000; background-color: #CAE4FF; border: 1px dotted #006600;");
                    sb.Append("                font-family: Arial; width: 625px;\">");
                    sb.Append("                <tr>");
                    sb.Append("                    <td style=\"width: 145px;\" valign=\"top\">");
                    sb.Append("                        Prescription(s)");
                    sb.Append("                    </td>");
                    sb.Append("                    <td>");
                    // Get priscription info
                    VisitData PV = new VisitData();
                    PV.VisitNumber = int.Parse(dtVisits.Rows[i]["VisitNumber"].ToString());
                    DataTable dt = PVLogic.GetVisitPrescription(PV);
                    string prescription = string.Empty;
                    for (int preRow = 0; preRow < dt.Rows.Count; preRow++)
                    {
                        prescription += "<a href=file://" + dt.Rows[preRow]["PrescriptionDocument"].ToString().Replace(" ","%20") + ">" + (preRow + 1).ToString() + "." + dt.Rows[preRow]["PrescriptionDocument"].ToString() +"</a> (Note:" + dt.Rows[preRow]["Note"].ToString() + ")";
                    }
                    sb.Append(prescription);
                    sb.Append("                    </td>");
                    sb.Append("                </tr>");
                    sb.Append("            </table>");
                    sb.Append("            <br />");
                    sb.Append("            <table style=\"font-size: 9pt; color: #000000; background-color: #CAE4FF; border: 1px dotted #006600;");
                    sb.Append("                font-family: Arial; width: 625px;\">");
                    sb.Append("                <tr>");
                    sb.Append("                    <td style=\"width: 145px;\" valign=\"top\">");
                    sb.Append("                        Dr&#39;s Dictation(s)");
                    sb.Append("                    </td>");
                    sb.Append("                    <td>");
                    // Get Dictation info
                    dt = PVLogic.GetVisitDrVoice(PV);
                    string dictation = string.Empty;
                    for (int preRow = 0; preRow < dt.Rows.Count; preRow++)
                    {
                        dictation += "<a href=file://" + dt.Rows[preRow]["DoctorVoiceComment"].ToString().Replace(" ", "%20") + ">" + (preRow + 1).ToString() + "." + dt.Rows[preRow]["DoctorVoiceComment"].ToString() + "</a> (Note:" + dt.Rows[preRow]["Note"].ToString() + ")";
                    }
                    sb.Append(dictation);
                    sb.Append("                    </td>");
                    sb.Append("                </tr>");
                    sb.Append("            </table>");
                    sb.Append("            <br />");
                    sb.Append("            <table style=\"font-size: 9pt; color: #000000; background-color: #CAE4FF; border: 1px dotted #006600;");
                    sb.Append("                font-family: Arial; width: 625px;\">");
                    sb.Append("                <tr>");
                    sb.Append("                    <td style=\"width: 145px;\" valign=\"top\">");
                    sb.Append("                        Additional Informations(s)");
                    sb.Append("                    </td>");
                    sb.Append("                    <td>");
                    // Get Additional info
                    dt = PVLogic.GetVisitAdditionalInfo(PV);
                    string addInfo = string.Empty;
                    for (int preRow = 0; preRow < dt.Rows.Count; preRow++)
                    {
                        addInfo += "<a href=file://" + dt.Rows[preRow]["AdditionalInformationDocument"].ToString().Replace(" ", "%20") + ">" + (preRow + 1).ToString() + "." + dt.Rows[preRow]["AdditionalInformationDocument"].ToString() + "</a> (Note:" + dt.Rows[preRow]["Note"].ToString() + ")";
                    }
                    sb.Append(addInfo);
                    sb.Append("                    </td>");
                    sb.Append("                </tr>");
                    sb.Append("            </table>");
                    sb.Append("        </div>");
                    sb.Append("    </div>");
                    sb.Append("    <br />");
                }
            }
            sb.Append("</body>");
            sb.Append("</html>");

            // Create the file.
            string fileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\VisitReport.html";
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            using (StreamWriter sw = new StreamWriter(fileName))
            {
                // Add some text to the file.
                sw.Write(sb.ToString());
            }
            ReportViewer rptView = new ReportViewer(fileName);
            rptView.Show();
        }
        #endregion General Methods

    }
}